package com.gmrz.uap.whitelist;

import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.ServiceSupport;
import com.gmrz.uap.model.WhiteList;
import com.gmrz.uap.util.Identities;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

public class WhiteListService extends ServiceSupport {

    public static final WhiteListService me = new WhiteListService();

    public static final String TABLE_PRIMARY_KEY = "wl_id";

    /**
     * 根据页面传过来的条件进行分页查询
     *
     * @param whiteList 白名单信息
     * @param page      页号
     * @param rows      行数
     * @param sort      排序字段
     * @param order     升序/降序
     * @return　分页结果 Page<WhiteList>
     */
    public Page<WhiteList> findWhiteListListForPaginate(WhiteList whiteList, int page, int rows, String sort, String order, String tenantid) {
        return WhiteList.dao.findWhiteListListForPaginate(whiteList, page, rows, sort, order,tenantid);
    }

    /**
     * 增加白名单信息
     *
     * @param whiteList 白名单信息
     * @return true/false
     */
    public boolean save(WhiteList whiteList) {
        whiteList.set(TABLE_PRIMARY_KEY, Identities.uuid2());
        return whiteList.save();
    }

    /**
     * 修改白名单信息
     *
     * @param whiteList 白名单信息
     * @return true/false
     */
    public boolean update(WhiteList whiteList) {
        return whiteList.update();
    }

    /**
     * 根据参数删除白名单信息，ID可以是多个，中间使用逗号隔开
     *
     * @param ids ID集合
     * @return true/false
     */
    @Before(Tx.class)
    public boolean remove(String ids) {
        String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);

        for (String id : idArr) {
            flag = WhiteList.dao.deleteById(id);
        }
        return flag;
    }

    public boolean exist(String visitAddress) {
        WhiteList whiteList = WhiteList.dao.getByVisitAdress(visitAddress);
        return null == whiteList;
    }


    public List<WhiteList> findAll(String tenantid){
        return WhiteList.dao.find("select * from t_white_list  where tenant_id = '"+tenantid+"'");
    }

    public WhiteList findOne(String ids){
        return WhiteList.dao.findById(ids);
    }

    public WhiteList getByVisitAdressAndAppids(String visitAdress, String appids){
        return WhiteList.dao.getByVisitAdressAndAppids(visitAdress,appids);
    }

}
